<?php

class StudentController extends BackendController
{

    public $layout='/layouts/column2';
    public $city_en;
    public function actionIndex()
    {
        header("Content-type: text/html; charset=utf-8");
        $model = new Student();
        $criteria = new CDbCriteria;
        $criteria->order = "id DESC";
        $dataProvider= new CActiveDataProvider($model, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));
        $this->render('index',array('dataProvider'=>$dataProvider));
    }

    public function actionDelete(){
        $id = Yii::app()->request->getParam('id');
        $info = Student::model()->findByPk($id);
        if($info){
            $ok = $info->delete();
            if($ok){
                $data['status'] = 1;
                $data['message'] = '删除数据成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '删除数据失败';
            }

        }else{
            $data['status'] = 0;
            $data['message'] = '你无权作此操作';
        }
        echo json_encode($data);
        Yii::app()->end();
    }

    public function actionUpdate(){
        $id = Yii::app()->request->getParam('id');
        Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
        //获取区域
        $info = Student::model()->findByPk($id);
        if(Yii::app()->request->isPostRequest){
            if($info){
                $_POST['Student']['user_name'] = trim($_POST['Student']['user_name']);
                $_POST['Student']['code'] = trim($_POST['Student']['code']);
                $info->setAttributes($_POST['Student']);
                if($info->update()){
                    //写入周边
                    $data['status'] = 1;
                    $data['message'] = '学生修改成功';
                }else{
                    $data['status'] = 0;
                    $data['message'] = $this->showError($info->getErrors());
                }
            }else{
                $data['status'] = 0;
                $data['message'] = '你无权作此操作';
            }

            echo json_encode($data);
            Yii::app()->end();
        }


        $this->layout = '/layouts/dialog';
        $this->render('update',array('info'=>$info));
    }
    public function actionAdd(){
        if(Yii::app()->request->isPostRequest){
            $model = new Student();
            $_POST['Student']['user_name'] = trim($_POST['Student']['user_name']);
            $_POST['Student']['code'] = trim($_POST['Student']['code']);
            $model->setAttributes($_POST['Student']);
            if($model->save()){
                $data['status'] = 1;
                $data['message'] = '发布学生成功';
            }else{
                $data['status'] = 0;
                $data['message'] = $this->showError($model->getErrors());
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
            $this->layout = '/layouts/dialog';
            $this->render('add');

        }
    }
    public function actionKAdd(){
        if(Yii::app()->request->isPostRequest){
            $model = new Student();
            $content_list = trim($_POST['content']);
            $content_list = str_replace("	"," ",$content_list);
            $content_arr = explode("\r\n", $content_list);
            foreach($content_arr as $v){
                $arr[] = explode(" ", $v);
            }
            foreach($arr as $vv){
                $model = new Student();
                $model->user_name = $vv[1];
                $model->code = $vv[0];
                $ok = $model->save();
            }
            if($ok){
                $data['status'] = 1;
                $data['message'] = '发布学生成功';
            }else{
                $data['status'] = 0;
                $data['message'] = $this->showError($model->getErrors());
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
            $this->layout = '/layouts/dialog';
            $this->render('kadd');

        }
    }

}